<template>
  <div :class=" propGoodsId ? '' : 'app-container'">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品编码" prop="goodsId" v-if="!propGoodsId">
        <el-input
          v-model="queryParams.goodsId"
          placeholder="请输入商品编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品名称" prop="goodsName" v-if="!propGoodsId">
        <el-input
          v-model="queryParams.goodsName"
          placeholder="请输入商品名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择类型" clearable size="small">
          <el-option v-for="it in handleOptions"
                     :value="Number(it.dictValue)" :label="it.dictLabel" />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option label="正常" :value="1" />
          <el-option label="作废" :value="2" />
        </el-select>
      </el-form-item>

      <el-form-item label="创建人" v-if="!propGoodsId">
        <el-input v-model="queryParams.createBy"
                  placeholder="请输入创建人"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="创建时间" v-if="!propGoodsId">
        <el-date-picker
          size="small"
          v-model="createTimeRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :unlink-panels="true"
          @change="createTimeChange"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['recovery:arrange:export']"
          v-if="!propGoodsId"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="arrangeList" border>
      <el-table-column label="序号" align="center" type="index" />
      <el-table-column label="仓库" align="center" prop="stockName" width="160" show-overflow-tooltip />
      <el-table-column label="商品编码" align="center" prop="goodsId" width="180" />
      <el-table-column label="商品名称" align="center" prop="goodsName" width="180" show-overflow-tooltip />
      <el-table-column label="类型" align="center" prop="type" width="120" :formatter="typeFormatter" />
      <el-table-column label="状态" align="center" width="120" prop="status">
        <template v-slot="scope">
          <el-tag type="success" v-if="scope.row.status === 1">正常</el-tag>
          <el-tag v-else type="info">作废</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createBy" show-overflow-tooltip />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180" />
      <el-table-column label="修改人" align="center" prop="updateBy" show-overflow-tooltip />
      <el-table-column label="修改时间" align="center" prop="updateTime" width="180" />
      <el-table-column label="备注" align="center" prop="remark" show-overflow-tooltip />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listArrange, exportArrange } from "@/api/recovery/arrange";

export default {
  name: "Arrange",
  components: {
  },
  props: {
    propGoodsId: {type: String},
  },
  watch: {
    propGoodsId(now, old) {
      if(now) {
        this.queryParams.goodsId = now;
        this.getList();
      }
    }
  },
  data() {
    return {
      handleOptions: [],
      createTimeRange: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 打理日志表格数据
      arrangeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        goodsId: this.propGoodsId,
        goodsName: null,
        type: null,
        status: null,
        createBy: null,
        params: {}
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("goods_handle_type").then(response => {
      this.handleOptions = response.data;
    });
  },
  methods: {
    typeFormatter(row) {
      return this.selectDictLabel(this.handleOptions, row.type);
    },
    /** 创建时间选项*/
    createTimeChange(e) {
      if(e == null) {
        this.$delete(this.queryParams.params, 'createStartTime');
        this.$delete(this.queryParams.params, 'createEndTime');
        return
      }
      this.$set(this.queryParams.params, 'createStartTime', e[0]);
      this.$set(this.queryParams.params, 'createEndTime', e[1]);
    },
    /** 查询打理日志列表 */
    getList() {
      this.loading = true;
      listArrange(this.queryParams).then(response => {
        this.arrangeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.createTimeRange = [];
      this.createTimeChange(null);
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有打理日志数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportArrange(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
